// nodeJs实现批量下载网站图片
/**
 * 思路：
 * 1、使用https模块发起请求，获取相应数据
 * 2、分析HTML数据，提取所有图片地址
 * 3、根据图片地址发起请求，获取图片保存在硬盘
 */
let https = require("https");
let fs = require("fs");
let path = require("path");
https.get("https://www.mmonly.cc/tag/mtw/", res => {
    let html = "";
    res.on("data", data => {
        html += data;
    })
    res.on("end", () => {
        let reg = /src="(.*?\.jpg|png)"/img;
        let url="";
        while(url=reg.exec(html)){
            downloadImg(url[1]);
        }
    });
});

let downloadImg = function(url){
    let obj = path.parse(url);
    let filename = obj.base;
    let writeStream = fs.createWriteStream(`./image/${filename}`);
    https.get(url,res=>{
        res.pipe(writeStream)
    });
}
